package lq1189;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		//输入数据量大，用BufferedReader
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] nums = br.readLine().split(" ");
		int n = Integer.parseInt(nums[0]);
		int m = Integer.parseInt(nums[1]);
		
		int[] s1 = new int[n];
		int[] s2 = new int[m];
		
		nums = br.readLine().split(" ");
		for(int i=0;i<n;i++) {
			s1[i] = Integer.parseInt(nums[i]);
		}
		
		nums = br.readLine().split(" ");
		for(int i=0;i<m;i++) {
			s2[i] = Integer.parseInt(nums[i]);
		}
		
		System.out.println(lcs(s1,s2));

	}

	//最长公共子序列LCS（模板）
	static int maxn = 1100;
	static int[][] dp = new int[maxn][maxn];
    static int lcs(int[] s1, int[] s2) {   
        int len1 = s1.length;
        int len2 = s2.length;
        for (int i = 1; i <= len1; i++) {
            for (int j = 1; j <= len2; j++) {
            	//字符串实际下标从0开始
                if (s1[i - 1] == s2[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
                }
            }
        }
        return dp[len1][len2];
    }
}
